package igloo.ui;

import igloo.aws.Region;
import igloo.controller.IglooController;
import igloo.controller.VaultController;

import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;

public class CreateVaultDialog extends JDialog
{
    private static final long serialVersionUID = 1L;

    private JLabel mNameLabel;
    private JTextField mNameTextField;

    private JLabel mRegionLabel;
    private JComboBox mRegionComboBox;

    private JButton mOkButton;
    private JButton mCancelButton;

    private IglooController mIglooController;
    private Region mRegion;

    public CreateVaultDialog(Frame frame, boolean modal,
            IglooController controller)
    {
        this(frame, modal, controller, null);
    }

    public CreateVaultDialog(Frame frame, boolean modal,
            IglooController controller, Region region)
    {
        super(frame, "Create Vault", modal);
        mIglooController = controller;
        mRegion = region;

        setResizable(false);

        JPanel panel = new JPanel();

        panel.setLayout(new MigLayout("fillx, hidemode 3"));

        panel.add(getRegionLabel(), "growx, wrap");
        panel.add(getRegionComboBox(), "growx, wrap");
        panel.add(getNameLabel(), "wrap");
        panel.add(getNameTextField(), "growx, wrap");
        panel.add(getOkButton(), "tag ok, align right, split");
        panel.add(getCancelButton(), "tag cancel");
        setContentPane(panel);

        if(region != null)
        {
            getRegionLabel().setVisible(false);
            getRegionComboBox().setVisible(false);
            getRegionComboBox().setSelectedItem(region);
        }
    }

    public String getVaultName()
    {
        return getNameTextField().getName();
    }

    public Region getRegion()
    {
        // TODO
        return Region.US_EAST_1;
    }

    private JLabel getNameLabel()
    {
        if(mNameLabel == null)
        {
            mNameLabel = new JLabel("Name:");
        }

        return mNameLabel;
    }

    private JTextField getNameTextField()
    {
        if(mNameTextField == null)
        {
            mNameTextField = new JTextField();
        }

        return mNameTextField;
    }

    private JLabel getRegionLabel()
    {
        if(mRegionLabel == null)
        {
            mRegionLabel = new JLabel("Region:");
        }

        return mRegionLabel;
    }

    private JComboBox getRegionComboBox()
    {
        if(mRegionComboBox == null)
        {
            mRegionComboBox = new JComboBox(Region.values());

            mRegionComboBox.addItemListener(new ItemListener() {
                @Override
                public void itemStateChanged(ItemEvent e)
                {
                    if(e.getStateChange() == ItemEvent.SELECTED)
                        mRegion = (Region)e.getItem();
                }
            });
        }

        return mRegionComboBox;
    }

    private JButton getOkButton()
    {
        if(mOkButton == null)
        {
            mOkButton = new JButton("OK");

            mOkButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    String name = getNameTextField().getText();

                    BusyPanel panel = new BusyPanel();
                    panel.setText("Creating vault...");
                    setContentPane(panel);
                    validate();

                    ListenableFuture<VaultController> future = mIglooController.createVault(
                            mRegion, name);
                    Futures.addCallback(future,
                            new FutureCallback<VaultController>() {
                                @Override
                                public void onSuccess(VaultController controller)
                                {
                                    EventQueue.invokeLater(new Runnable() {
                                        @Override
                                        public void run()
                                        {
                                            dispose();
                                        }
                                    });
                                }

                                @Override
                                public void onFailure(Throwable t)
                                {
                                    t.printStackTrace();
                                }
                            });
                }
            });
        }

        return mOkButton;
    }

    private JButton getCancelButton()
    {
        if(mCancelButton == null)
        {
            mCancelButton = new JButton("Cancel");

            mCancelButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    dispose();
                }
            });
        }

        return mCancelButton;
    }

    public static void main(String[] args)
    {
        CreateVaultDialog dlg = new CreateVaultDialog(null, true, null);
        dlg.setSize(250, 250);
        dlg.setVisible(true);
    }
}
